package me.zhengjie.kwService.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import me.zhengjie.constant.KwConstant;
import me.zhengjie.kwService.PhoneService;
import me.zhengjie.mapper.MasterTearcherMapper;
import me.zhengjie.mapper.PublicExamMapper;
import me.zhengjie.mapper.StudentMapper;
import me.zhengjie.pojo.dto.PhoneLoginDto;
import me.zhengjie.pojo.entity.MasterTearcher;
import me.zhengjie.pojo.entity.PublicExam;
import me.zhengjie.pojo.entity.Student;
import me.zhengjie.util.AssertUtils;
import me.zhengjie.util.BaseResponse;
import me.zhengjie.util.RedisUtils;
import me.zhengjie.util.UUIDUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;

/**
 * @author Long
 * @date 2021/7/6 10:10
 */
@Service
@Slf4j
public class PhoneServiceImpl implements PhoneService {

    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private MasterTearcherMapper masterTearcherMapper;
    @Autowired
    private PublicExamMapper publicExamMapper;


    @Override
    public BaseResponse phoneLogin(PhoneLoginDto dto) {
        AssertUtils.isNotNull(dto, "登录信息不可为空");
        AssertUtils.isNotNull(dto.getIdCard(), "身份证号不可为空");
        AssertUtils.isNotNull(dto.getName(), "姓名不可为空");
        QueryWrapper<Student> wrapper = new QueryWrapper<>();
        wrapper.eq("id_card", dto.getIdCard())
                .eq("name_cn", dto.getName());
        Student student = studentMapper.selectOne(wrapper);
        if (student != null) {
            String userToken = UUIDUtils.getUUID();
            String value = JSON.toJSONString(student);
            /** 把登录用户转JSON存储到redis中,设置过期时间为60分钟*/
            RedisUtils.INSTANCE.set(userToken, value, 3600);
            HashMap<String, String> map = new HashMap<>();
            /** redisKey*/
            map.put(KwConstant.USERTOKEN, userToken);
            /** 账号身份*/
            map.put("identity", "student");
            return BaseResponse.ok("登录成功", map);
        }
        QueryWrapper<MasterTearcher> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id_card", dto.getIdCard())
                .eq("name_cn", dto.getName());
        MasterTearcher masterTearcher = masterTearcherMapper.selectOne(queryWrapper);
        if (masterTearcher != null) {
            String userToken = UUIDUtils.getUUID();
            String value = JSON.toJSONString(masterTearcher);
            /** 把登录用户转JSON存储到redis中,设置过期时间为60分钟*/
            RedisUtils.INSTANCE.set(userToken, value, 3600);
            HashMap<String, String> map = new HashMap<>();
            /** redisKey*/
            map.put(KwConstant.USERTOKEN, userToken);
            /** 账号身份*/
            if (masterTearcher.getIdentity().contains("0") && masterTearcher.getIdentity().contains("1")) {
                map.put("identity","teacher,staff");
                return BaseResponse.ok("登录成功",map);
            }
            map.put("identity", "0".equals(masterTearcher.getIdentity()) ? "teacher" : "staff");
            return BaseResponse.ok("登录成功", map);
        }
        return BaseResponse.failed("该用户不存在");
    }

    @Override
    public PublicExam getNewPublicExam() {


        return publicExamMapper.getNewPublicExam();
    }
}
